<?php 
/**
*	wps对接在线文档接口回调的
*/
class v1ClassAction extends apiAction
{
	

	public function int3rdAction()
	{
		$this->apipath 	= arrvalue($GLOBALS,'_paths');
		if(!isempt($this->get('_w_yingkey'))){
			$laiyuan= $this->get('_w_cfroace');
			$url 	= 'http://file.rockoa.com';
			if($laiyuan=='dev')$url 	= 'http://rock.vaiwan.com/app/rockfile';
			$surl 	= $_SERVER['REQUEST_URI'];
			$tourl 	= ''.$url.''.$surl.'';
			if(contain($this->apipath,'file/save')){
				$fileurl = $this->getsavefile();
				if($fileurl)$tourl.='&fileurl='.$this->jm->base64encode($fileurl).'';
			}
			$cont = file_get_contents($tourl);
			//$this->rock->debugs($cont.chr(10).$tourl, 'getcont');
			return $cont;
		}			
		
		$this->filenum 	= getheader('X-Weboffice-File-Id');
		$this->frs		= m('file')->getone("`filenum`='$this->filenum'");
		$fileext 		= arrvalue($this->frs, 'fileext');
		$office			= ',doc,docx,ppt,pptx,xls,xlsx,';
		if(!contain($office,','.$fileext.','))return 'notfile';
		
		$this->optid 	= $this->get('_w_optid','0'); //当前操作人id
		$this->optrs	= m('usera')->getone('`agentid`='.$this->agentid.' and `uid`='.$this->optid.'');
		//$this->rock->debugs($_SERVER, str_replace('/','_', $this->apipath));
		
		if(contain($this->apipath,'file/info'))return $this->fileinfo();
		if(contain($this->apipath,'file/save'))return $this->filesave();
		if(contain($this->apipath,'file/history'))return $this->filehistory();
		
		return 'sorry not mess';
	}
	
	
	//获取文件元数据
	private function fileinfo()
	{
		$filepath 		= $this->frs['filepath'];
		$filepathnew	= $this->frs['filepathnew'];
		$url 			= getconfig('outurl', URL);
		$url1 			= getconfig('outurlm'); 
		if($url1)$url   = $url1;
		$filename 		= $this->frs['filename'];
		
		if(!isempt($filepathnew) && file_exists($filepathnew))$filepath=$filepathnew;
		
		$file['id'] 	= $this->filenum;
		$file['name'] 	= $filename;
		$file['version']= (int)$this->frs['version'];;
		$file['size'] 	= (int)$this->frs['filesize'];
		$file['creator']= $this->frs['optid'];
		$file['create_time'] = strtotime($this->frs['adddt']);
		$file['modify_time'] = time();
		$file['modifier'] 	 = $this->optid;
		
		$download_url = $filepath;
		if(substr($filepath,0,4)!='http')$download_url = ''.$url.''.$filepath.'';
		$file['download_url']= $download_url;
		
		$permission	= $this->get('_w_permission','read');
		
		$file['user_acl'] = array(
			'rename' 	=> 0,
			'history' 	=> 0,
			'export' 	=> 1,
			'copy' 		=> 1,
			'print' 	=> 1
		);
		$file['watermark'] = array(
			'type' => 0,
			'value' => '',
			'fillstyle' => 'rgba( 192, 192, 192, 0.6 )',
			'font' => 'bold 16px Serif',
			'rotate' => -0.7853982,
			'horizontal' => 50,
			'vertical' => 100,
		);
		//预览时加水印
		if($permission=='read'){
			$file['watermark']['type']=1;
			$file['watermark']['value']=arrvalue($this->optrs,'name');
		}
		
		$user = array(
			'id' 	=> $this->optid,
			'name' 	=> arrvalue($this->optrs,'name'),
			'permission' => $permission,
			'avatar_url' => $this->getface(arrvalue($this->optrs,'face')),
		);
		//$this->rock->debugs($file, 'fileinof');
		return array(
			'file'=>$file,
			'user'=>$user,
		);
	}
	
	//获取保存文件路径
	private function getsavefile()
	{
		$upobj 		= c('upfile');
		$upobj->initupfile('*', ''.UPDIR.'/'.date('Y-m').'', 50);
		$barr		= $upobj->up('file');
		if(is_string($barr)){
			return '';
		}
		$filepath	= getconfig('outurl', URL).$barr['allfilename'];
		return $filepath;
	}
	
	private function getface($face)
	{
		$url 			= getconfig('outurl', URL);
		if(isempt($face))$face = 'images/noface.png';
		if(substr($face,0,4)!='http')$face = ''.$url.''.$face.'';
		return $face;
	}
	
	/**
	*	编辑完成保存
	*/
	public function filesave()
	{
		$filepath 		= $this->frs['filepath'];
		$filename 		= $this->frs['filename'];

		$upobj 		= c('upfile');
		$upobj->initupfile('*', ''.UPDIR.'/'.date('Y-m').'', 50);
		$filena 	= 'wps'.$this->usersrs['xinhuid'].'_'.$this->frs['fileid'].'_'.date('dHis').'_'.rand(1000,9999).'';
		$barr		= $upobj->up('file', $filena);
		if(is_string($barr)){
			$this->rock->debugs($barr, 'saveerr');
			return returnerror($barr);
		}
		$banb 		= (int)$this->frs['version'];
		$filepath	= $barr['allfilename'];
		$this->filesaves($filepath, $this->frs['fileext']);

		$file = array(
			'id' 		=> $this->filenum,
			'name' 		=> $filename,
			'version' 	=> $banb+1,
			'size' 		=> $barr['filesize'],
			'download_url' => ''.getconfig('outurl', URL).''.$filepath.''
		);
		return array(
			'file'=>$file
		);
	}
	private function filesaves($filepath, $fileext)
	{
		$banb 			 = (int)$this->frs['version'];
		$uarr['mid'] 	 = $this->frs['id'];
		$uarr['agentid'] = $this->agentid;
		$uarr['platid']  = $this->platid;
		$uarr['optid'] 	 = $this->optid;
		$uarr['optname'] = arrvalue($this->optrs,'name');
		$uarr['filepath']= $filepath;
		$uarr['version'] = $banb+1;
		$uarr['filesize']= filesize($filepath);
		$uarr['optdt'] 	 = $this->rock->now;
		$ver = m('files')->insert($uarr);
		$oldfilepath	 = $this->frs['filepath'];
		$oldfilepath1	 = $this->frs['filepathnew'];
		
		m('file')->update("`filepathnew`='$filepath',`version`=`version`+1,`fileext`='$fileext',`lastdt`='{$this->rock->now}',`lastid`='{$this->optid}'", $uarr['mid']);//更正最新版路径
		
		//异步推送OA，或者OA主动抓取
		$callurl = $this->get('_w_callurl');
		if(isempt($callurl))$callurl = $this->get('callurl');
		if(!isempt($callurl)){
			$callurl = $this->jm->base64decode($callurl);
			$jgs	 = contain($callurl,'?') ? '&' : '?';
			$callurl.= ''.$jgs.'fileext='.$fileext.'';
			$data 	 = base64_encode(file_get_contents($filepath));
			$result	 = c('curl')->postcurl($callurl, $data);
			if(contain($result,'ok')){
				m('files')->update('`isup`=1', $ver);
				//删除缓存文件
				if(!isempt($oldfilepath) && file_exists($oldfilepath))unlink($oldfilepath);
				if(!isempt($oldfilepath1) && file_exists($oldfilepath1))unlink($oldfilepath1);
			}else{
				$this->rock->debugs(''.$callurl.':=>'.$result.'', 'callurlerr');
				$dizhi = substr($callurl, 0, strripos($callurl, '/'));
				m('files')->update("`explain`='无法回传地址“".$dizhi."”:".$result."',`isup`=0", $ver);
			}
		}
	}
	
	/**
	*	onlyoffice 编辑回调
	*/
	public function onlyofficecallAction()
	{
		$result = $this->getpostdata();
		$data   = json_decode($result, true);
		$status = arrvalue($data,'status');
		//$this->rock->debugs($result,'onlyofficecall');
		
		$this->filenum 	= $this->get('filenum');
		$this->frs		= m('file')->getone("`filenum`='$this->filenum'");
		$this->optid 	= $this->get('optid','0');
		$this->optrs	= m('usera')->getone('`agentid`='.$this->agentid.' and `uid`='.$this->optid.'');
		
		$newext = $this->frs['fileext'];
		if($newext=='xls')$newext = 'xlsx';
		if($newext=='doc')$newext = 'docx';
		if($newext=='ppt')$newext = 'pptx';
		
		//打开编辑时
		if($status=='1'){
		}
		
		//编辑完成
		if($status=='2'){
			$nurl = $data['url'];
			$filepath = ''.UPDIR.'/'.date('Y-m').'/only'.$this->usersrs['xinhuid'].'_'.$this->frs['fileid'].'_'.date('dHis').'_'.rand(1000,9999).'.'.$newext.'';
			$cont 	  = @file_get_contents($nurl);
			if(!$cont)return '{"error":1,"msg":"没用文件"}';
			$bo = $this->rock->createtxt($filepath, $cont);
			if(!$cont)return '{"error":1,"msg":"保存失败"}';
			
			$this->filesaves($filepath, $newext);
		}
		
		return '{"error":0}';
	}
	
	/**
	*	获取历史版本
	*/
	public function filehistory()
	{
		$data = json_decode($this->getpostdata(), true);
		
		$mid  = $this->frs['id']; 
		$rows = m('files')->getall('`mid`='.$mid.'','*','id desc');
		$histories 		= array();
		$url 			= getconfig('outurl', URL);
		$create_time	= strtotime($this->frs['adddt']);
		$usdb			= m('usera');
		$crtrs 			= $usdb->getone($this->frs['optid']);
		foreach($rows as $k=>$rs){
			$edrs 			= $usdb->getone($rs['optid']);
			$histories[] = array(
				'id' 	=> $data['id'],
				'name' 	=> $this->frs['filename'],
				'version' => 2,
				'size' 	=> (int)$rs['filesize'],
				'download_url' => ''.$url.''.$rs['filepath'].'',
				'create_time' => $create_time,
				'modify_time' => strtotime($rs['optdt']),
				'creator' => array(
					'id' => $this->frs['optid'],
					'name' => $this->frs['optname'],
					'avatar_url' => $this->getface(arrvalue($crtrs,'face')),
				),
				'modifier' => array(
					'id' => $rs['optid'],
					'name' => $rs['optname'],
					'avatar_url' => $this->getface(arrvalue($edrs,'face')),
				)
			);
		}
		$histories = array(
			'histories' => $histories
		);
		//$this->rock->debugs($histories, 'history');
		return $histories;
	}
}